import java.nio.ByteBuffer;


public class DockingCircle {

	public int x;
	public int y;
	public int radius;
	public long detectTimeStamp = -1;
	public long firstNotDetectedTimeStamp = -1;
	public long currentNotDetectedTimeStamp = -1;
	public long transmitTimeStamp = -1;
	public boolean detected;
	public int relativeX;
	public int relativeY;
	public byte XY;	
	Main delegate;
	
	public DockingCircle(Main $delegate){
		delegate = $delegate;
		x = -1;
		y = -1;
		radius = -1;
		
	}
	
	public void update(int _x, int _y, int _radius,int screenX, int screenY){
		
		boolean firstRequest = false;
		
		if(x == -1 && y == -1)
			firstRequest = true;
		
		detectTimeStamp = System.currentTimeMillis();
		x = _x;
		y = _y;
		radius = _radius;
		translate(screenX, screenY);
		System.out.println("Translated to "+ relativeX + " " + relativeY);
		detected = true;
		
		if(firstRequest){
			sendUpdate();
		}
	}
	
	public void translate(int screenX, int screenY){
			//translates point relative to screen size
		//					y
		//					|7
		//					|
		//					|
		//					|
		//					|
		//					|
		//- 7 ------------------------------ 7 x
		//					|
		//					|
		//					|
		//					|
		//					|
		//					|-7
		
		
		//  ------------------------------  x
		//	|
		//	|
		//	|
		//	|
		//	|
		//	|
		//	|
		//	|
		//	|
		//	|
		//	|
		//  y 
		
		int originx = screenX/2;
		int originy = screenY/2;
		
		relativeX = x - originx;
		relativeY = -(y - originy);
		
		int scaleX = (screenX/2)/7;
		int scaleY = (screenY/2)/7;

		relativeX = relativeX/scaleX;
		relativeY = relativeY/scaleY;

		byteTranslation();
		
		/*
		if(this.transmitTimeStamp == -1 ){
			sendUpdate();
		}else if(detectTimeStamp - transmitTimeStamp > 50){//at least .05 seconds between updates
			sendUpdate();
		}*/
	}
	
	public void byteTranslation(){
		byte X = (ByteBuffer.allocate(4).putInt(relativeX).array())[3];
		byte Y = (ByteBuffer.allocate(4).putInt(relativeY).array())[3];
		
		System.out.println("x = " + relativeX + " , y = " + relativeY);
		System.out.println( "-> "
                + Integer.toBinaryString(X) + ", " + Integer.toBinaryString(Y));
		
		XY = 0x0;
		X = (byte) (X << 4 & 0xf0);
		XY = (byte) (X ^ (Y & 0x0f));
		
		System.out.println( "-> "
                + Integer.toBinaryString(XY));
		
	}
	
	public void sendUpdate(){
		delegate.sendDockingOffset();
	}
	
	public void getUpdate(){
		System.currentTimeMillis();
		if(Math.abs(this.detectTimeStamp - this.firstNotDetectedTimeStamp) < 2000){
			delegate.sendDockingOffset();
		}else{
			delegate.sendNoDock();
		}
	}

	public void notDetected() {
		
		if(detected == true){
			
			firstNotDetectedTimeStamp = System.currentTimeMillis();
			currentNotDetectedTimeStamp = firstNotDetectedTimeStamp;
			detected = false;
			
		}else{
			
			
			currentNotDetectedTimeStamp = System.currentTimeMillis();
			
			if(currentNotDetectedTimeStamp - firstNotDetectedTimeStamp > 2000  && detectTimeStamp == -1){//special case, where circle is never even initially detected
				delegate.sendNoDock();
			}
		}
	}
	
	
}
